Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return HTTP response codes as typed errors #161

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

suhlig
Copy link

@suhlig suhlig commented Aug 7, 2021

This allows to differentiate a datasource not found from other errors, e.g.

_, err = client.GetDatasource(ctx, dsID)

if errors.Is(err, sdk.ErrNotFound) {
    fmt.Fprintf(os.Stderr, "Creating new datasource %s (id=%d)\n", ds.Name, ds.ID)
}

Copy link
Collaborator

@GiedriusS GiedriusS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff! Maybe you could do this for other REST calls as well? Plus, could you please update README.md that Go >=1.13 is required?

)

var (
ErrNotFound = errors.New("not found")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe these could go into rest-common.go or something? 🤔

@suhlig
Copy link
Author

suhlig commented Aug 10, 2021

Thanks for the feedback. I'll be back with some updates to this PR.

@suhlig suhlig force-pushed the datasources-use-status-code branch from 99de677 to 4f854eb Compare August 15, 2021 11:53
@suhlig
Copy link
Author

suhlig commented Aug 15, 2021

I amended my commit with the changes you proposed. Test are green on my workstation; not sure how I could run the GitHub actions.

@GiedriusS
Copy link
Collaborator

It seems like linters are failing. Could you please take a look?

@suhlig suhlig force-pushed the datasources-use-status-code branch from 4f854eb to 0e3e593 Compare August 17, 2021 17:03
@suhlig
Copy link
Author

suhlig commented Aug 17, 2021

Should be fixed now.

@GiedriusS
Copy link
Collaborator

Still, some errors are happening. AFAICT the issue is here:
https://github.com/grafana-tools/sdk/blob/master/rest-dashboard_integration_test.go#L87-L90

We should ignore 404 errors here (: @suhlig could you please take a look?

@suhlig
Copy link
Author

suhlig commented Aug 18, 2021

Sure! Thanks for catching these. I ran go test ./...; shouldn't that have shown the error?

@GiedriusS
Copy link
Collaborator

We also have integration tests. For that, you need a locally running Grafana, most likely in a Docker container, plus environment variable GRAFANA_INTEGRATION set to 1. You can see how it works here.

This allows to differentiate a datasource not found from other errors, e.g.

```go
_, err = client.GetDatasource(ctx, dsID)

if errors.Is(err, sdk.ErrNotFound) {
    fmt.Fprintf(os.Stderr, "Creating new datasource %s (id=%d)\n", ds.Name, ds.ID)
}
```
@suhlig suhlig force-pushed the datasources-use-status-code branch from 0e3e593 to 66a7bce Compare August 19, 2021 21:48
@suhlig
Copy link
Author

suhlig commented Aug 19, 2021

The integration test failure turned out to be a valuable one. I had overlooked a few error cases for delete-dashboard-by-uid and get-dashboard-by-uid.

I changed the integration test to consistently use DeleteDashboardByUID because this one has the 404 documented, whereas delete-dashboard-by-slug (DELETE /api/dashboards/db/:slug) as used by DeleteDashboard is deprecated since Grafana 5.0.

I also added instructions to the README on how to run local tests.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.7%) to 46.252% when pulling 66a7bce on suhlig:datasources-use-status-code into 9de4d14 on grafana-tools:master.

@GiedriusS
Copy link
Collaborator

Thanks, I will take a look tomorrow

Copy link
Collaborator

@GiedriusS GiedriusS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, except for one suggestion. Let me know if it makes sense

err = dec.Decode(&result)
boardBytes = []byte(result.Board)
case http.StatusNotFound:
err = fmt.Errorf("dashboard with path %q %w", path, ErrNotFound)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could move these switches into a function and then consistently use it when the HTTP status code is not equal to 200? The function could accept some string indicating the action or extra data. Then we could do:
return httpError(fmt.Sprintf("searching for dashboard with path %q", path))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestions. I will look into this when I am back from vacation.

Copy link
Collaborator

@GiedriusS GiedriusS Aug 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I really think that this improvement is worthwhile because the users will be able to use errors.Is(err, ErrNotFound) and others consistently with all methods

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added a second commit that extracts httpStatusCodeError as suggested. Feel free to squash both commits if you are happy with the result.

Copy link
Collaborator

@GiedriusS GiedriusS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, just one suggestion.

return StatusMessage{}, err
}
if code != http.StatusOK {
return StatusMessage{}, fmt.Errorf("HTTP error %d: returns %s", code, raw)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use httpStatusCodeError everywhere in all of these functions (in this and others)?

@suhlig
Copy link
Author

suhlig commented Nov 3, 2021

Bump ... anything I need to do before the PR can be merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants